package com.qqtech.qquan.user.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.model.UserValueQuery;
import com.qqtech.qquan.user.model.UserValueVo;

@Repository
public class UserValueDaoImpl extends BaseDaoImpl<UserValue> implements UserValueDao {
	public static final Logger log = LoggerFactory.getLogger(UserValueDaoImpl.class);

	@Override
	public int queryCountByDistance(UserValue query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountByDistance"), BeanUtil.toMap(query));
	}

	@Override
	public int queryCountUserInfo(UserValueQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountUserInfo"), BeanUtil.toMap(query));
	}

	@Override
	public Page<UserValueVo> queryUserInfo(UserValueQuery query, Pageable pageable) {
		List<UserValueVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectUserInfo"),
				getParams(query, pageable));
		return new PageImpl<UserValueVo>(contentList, pageable, this.queryCountUserInfo(query));
	}

	@Override
	public Page<UserValueVo> queryPageListByDistance(UserValue query, Pageable pageable) {
		List<UserValueVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectByDistance"),
				getParams(query, pageable));
		return new PageImpl<UserValueVo>(contentList, pageable, this.queryCountByDistance(query));
	}

	@Override
	public void alterMoney(int userId, int alterValue) {
		UserValue userValue = new UserValue();
		userValue.setId(userId);
		userValue.setMoney(alterValue);
		sqlSessionTemplate.update(getSqlName("alterMoneyById"), userValue);
	}

	@Override
	public void alterQuanValue(int userId, int alterQuanValue) {
		UserValue userValue = new UserValue();
		userValue.setId(userId);
		userValue.setQuanValue(alterQuanValue);
		sqlSessionTemplate.update(getSqlName("alterQuanValueById"), userValue);
	}

	@Override
	public void alterHonorValue(int userId, int alterHonorValue, Integer honorLevel, String honorLevelName) {
		UserValue userValue = new UserValue();
		userValue.setId(userId);
		userValue.setHonorValue(alterHonorValue);
		userValue.setHonorLevel(honorLevel);
		userValue.setHonorLevelName(honorLevelName);
		sqlSessionTemplate.update(getSqlName("alterHonorValueById"), userValue);
	}

	@Override
	public void alterRegistrationId(int userId, String registrationId) {
		UserValue userValue = new UserValue();
		userValue.setId(userId);
		userValue.setRegistrationId(registrationId);
		sqlSessionTemplate.update(getSqlName("alterRegistrationIdById"), userValue);
	}

	@Override
	public int updateDrawCount(Integer id) {
		return sqlSessionTemplate.update(getSqlName("updateDrawCount"), id);
	}

	@Override
	public int updateMinusDrawCount(Integer id) {
		return sqlSessionTemplate.update(getSqlName("updateMinusDrawCount"), id);
	}

	@Override
	public Page<UserValueVo> queryUserInfoByTalent(UserValueQuery query, Pageable pageable) {
		List<UserValueVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectUserByTalentCount"),
				getParams(query, pageable));
		return new PageImpl<UserValueVo>(contentList, pageable, this.queryCountUserInfo(query));
	}

	@Override
	public List<UserValueVo> queryUserInfoByUserIds(UserValueQuery query) {
		List<UserValueVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectUserByUserIds"),
				BeanUtil.toMap(query));
		return  contentList;
	}
}
